# coding = utf-8

'''
展示数据的代码片段
'''

import os
import numpy as np
import matplotlib.pyplot as plt
from  PIL import Image

from starter_code.utils import load_case_by_path
from starter_code.visualize import visualizeFromPath,hu_to_grayscale



DATA_PATH = "E:\数据集\Kidney\kits19\data"
DEFAULT_HU_MAX = 512
DEFAULT_HU_MIN = -512

def read_data(index):
    total_path = os.path.join(DATA_PATH, "case_{}".format(str(index).zfill(5)))
    print(total_path)
    volume, segmentation = load_case_by_path(total_path)

    volume = volume.get_fdata()
    segmentation = segmentation.get_fdata()
    segmentation = segmentation.astype(np.int32)

    kidney = 0
    tumor = 0
    for i in range(segmentation.shape[0]):
        item = segmentation[i]
        total1 = item.sum()
        if total1 == 0:
            continue
        item[item == 1] = 0
        total2 = item.sum()
        item[item==2] = 0
        total3 = item.sum()

        if total2 < total1 :
            kidney += 1
        if total3 < total2:
            tumor += 1

    return (volume.shape[0],kidney,tumor)


def analysis_data():
    slice = []
    kidney = []
    tumor = []
    for i in range(210):
        (slice_number, kidney_num, tumor_num) = read_data(index=i)
        slice.append(slice_number)
        kidney.append(kidney_num)
        tumor.append(tumor_num)

    slice = np.array(slice)
    kidney = np.array(kidney)
    tumor = np.array(tumor)

    print(np.min(slice), np.max(slice), np.average(slice))
    print(np.min(kidney), np.max(kidney), np.average(kidney))
    print(np.min(tumor), np.max(tumor), np.average(tumor))








if __name__ == '__main__':
    analysis_data()
